package com.epalmpay.service.activity.impl;

import java.util.ArrayList;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.epalmpay.dto.activity.ActivityLotteryAwardInDTO;
import com.epalmpay.dto.activity.ActivityLotteryInDTO;
import com.epalmpay.dto.activity.ActivityLotteryListOutDTO;
import com.epalmpay.dto.activity.ActivityLotteryRecordInDTO;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.entity.ActivityLottery;
import com.epalmpay.entity.ActivityLotteryAward;
import com.epalmpay.entity.ActivityLotteryRecord;
import com.epalmpay.entity.Page;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.mapper.ActivityLotteryAwardMapper;
import com.epalmpay.mapper.ActivityLotteryMapper;
import com.epalmpay.mapper.ActivityLotteryRecordMapper;
import com.epalmpay.service.activity.ILotteryService;
import com.epalmpay.status.Rescode;
import com.epalmpay.util.DateUtil;
import com.epalmpay.util.Tools;

@Service("lotteryService")
public class LotteryServiceImpl implements ILotteryService{
	
	
	@Resource
	private  ActivityLotteryMapper activityLotteryMapper;
	@Resource
	private  ActivityLotteryAwardMapper activityLotteryAwardMapper;
	@Resource
	private  ActivityLotteryRecordMapper activityLotteryRecordMapper;
	

	@Override
	public ActivityLotteryListOutDTO lotteryList(ActivityLotteryInDTO lotteryInDTO, PageDTO pageDTO) throws ApplicationException {
		ActivityLotteryListOutDTO activityLotteryListOutDTO = new ActivityLotteryListOutDTO();
		Page page = Tools.pageEmtity(pageDTO);
		lotteryInDTO.setPage(page);
		ArrayList<ActivityLottery> activityLotteryOutDTO = activityLotteryMapper.selectActivityLotterylistPage(lotteryInDTO);
		activityLotteryListOutDTO.setActivityLotteryList(activityLotteryOutDTO);
		activityLotteryListOutDTO.setPage(page);
		return activityLotteryListOutDTO;
	}


	@Override
	public BaseOutDTO saveActivityLottery(ActivityLotteryInDTO activityLotteryInDTO) throws ApplicationException {
		BaseOutDTO baseOutDTO=new BaseOutDTO();
		
		ActivityLottery activityLottery=new ActivityLottery();
		activityLottery.setStartTime(DateUtil.fomatDate(activityLotteryInDTO.getStartTime(), DateUtil.sdfDay));
		activityLottery.setEndTime(DateUtil.fomatDate(activityLotteryInDTO.getEndTime(), DateUtil.sdfDay));
		activityLottery.setGameContent(activityLotteryInDTO.getGameContent());
		activityLottery.setGameName(activityLotteryInDTO.getGameName());
		activityLottery.setGameType(activityLotteryInDTO.getGameType());
		activityLottery.setGameWinRules(activityLotteryInDTO.getGameWinRules());
		activityLottery.setGameWinTips(activityLotteryInDTO.getGameWinTips());
		activityLottery.setGmtModified(new Date());
		activityLottery.setGmtCreate(new Date());
		activityLottery.setGroupId(activityLotteryInDTO.getGroupId());
		activityLottery.setIsScene(activityLotteryInDTO.getIsScene());
		activityLottery.setMaxPlayNum(activityLotteryInDTO.getMaxPlayNum());
		activityLottery.setPlayNum(0);
		activityLottery.setStatus(activityLotteryInDTO.getStatus());
		
		activityLotteryMapper.insertSelective(activityLottery);
		baseOutDTO.setErrorCode(Rescode.SUCCESS, "保存成功");
		return baseOutDTO;
	}


	@Override
	public ActivityLottery selectActivityLotteryId(Long id) throws ApplicationException {
		ActivityLottery activityLottery=activityLotteryMapper.selectByPrimaryKey(id);
		activityLottery.setBeginTime(DateUtil.fomatDate(activityLottery.getStartTime(), DateUtil.sdfDay));
		activityLottery.setFinishTime(DateUtil.fomatDate(activityLottery.getEndTime(), DateUtil.sdfDay));
		return activityLottery;
	}


	@Override
	public BaseOutDTO updateActivityLottery(ActivityLotteryInDTO activityLotteryInDTO) throws ApplicationException {
		BaseOutDTO baseOutDTO=new BaseOutDTO();
		ActivityLottery activityLottery=activityLotteryMapper.selectByPrimaryKey(activityLotteryInDTO.getId());
		activityLottery.setStartTime(DateUtil.fomatDate(activityLotteryInDTO.getStartTime(), DateUtil.sdfDay));
		activityLottery.setEndTime(DateUtil.fomatDate(activityLotteryInDTO.getEndTime(), DateUtil.sdfDay));
		activityLottery.setGameContent(activityLotteryInDTO.getGameContent());
		activityLottery.setGameName(activityLotteryInDTO.getGameName());
		activityLottery.setGameType(activityLotteryInDTO.getGameType());
		activityLottery.setGameWinRules(activityLotteryInDTO.getGameWinRules());
		activityLottery.setGameWinTips(activityLotteryInDTO.getGameWinTips());
		activityLottery.setGmtModified(new Date());
		activityLottery.setGroupId(activityLotteryInDTO.getGroupId());
		activityLottery.setIsScene(activityLotteryInDTO.getIsScene());
		activityLottery.setMaxPlayNum(activityLotteryInDTO.getMaxPlayNum());
		activityLottery.setPlayNum(activityLotteryInDTO.getPlayNum());
		activityLottery.setStatus(activityLotteryInDTO.getStatus());
		activityLotteryMapper.updateByPrimaryKeySelective(activityLottery);
		baseOutDTO.setErrorCode(Rescode.SUCCESS, "保存成功");
		return baseOutDTO;
	}


	@Override
	public ActivityLotteryListOutDTO activityLotteryAwardList(ActivityLotteryAwardInDTO activityLotteryAwardInDTO)
			throws ApplicationException {
		ArrayList<ActivityLotteryAward> list=activityLotteryAwardMapper.selectByLotteryIdList(activityLotteryAwardInDTO.getLotteryId());
		ActivityLotteryListOutDTO activityLotteryListOutDTO=new ActivityLotteryListOutDTO();
		activityLotteryListOutDTO.setActivityLotteryAwardList(list);
		return activityLotteryListOutDTO;
	}


	@Override
	public BaseOutDTO saveActivityLotteryAward(ActivityLotteryAwardInDTO activityLotteryAwardInDTO)
			throws ApplicationException {
		BaseOutDTO baseOutDTO=new BaseOutDTO();
		ActivityLotteryAward activityLotteryAward=new ActivityLotteryAward();
		activityLotteryAward.setAwardCount(activityLotteryAwardInDTO.getAwardCount());
		activityLotteryAward.setAwardLevel(activityLotteryAwardInDTO.getAwardLevel());
		activityLotteryAward.setAwardName(activityLotteryAwardInDTO.getAwardName());
		activityLotteryAward.setAwardProb(activityLotteryAwardInDTO.getAwardProb());
		activityLotteryAward.setGmtCreate(new Date());
		activityLotteryAward.setLotteryId(activityLotteryAwardInDTO.getLotteryId());
		activityLotteryAward.setRemainCount(activityLotteryAwardInDTO.getAwardCount());//创建的时候剩余数量为创建的数量
		activityLotteryAwardMapper.insertSelective(activityLotteryAward);
		baseOutDTO.setErrorCode(Rescode.SUCCESS, "保存成功");
		return baseOutDTO;
	}
	
	
	@Override
	public BaseOutDTO updateActivityLotteryAward(ActivityLotteryAwardInDTO activityLotteryAwardInDTO)
			throws ApplicationException {
		BaseOutDTO baseOutDTO=new BaseOutDTO();
		ActivityLotteryAward activityLotteryAward=activityLotteryAwardMapper.selectByPrimaryKey(activityLotteryAwardInDTO.getId());
		activityLotteryAward.setAwardCount(activityLotteryAwardInDTO.getAwardCount());
		activityLotteryAward.setAwardLevel(activityLotteryAwardInDTO.getAwardLevel());
		activityLotteryAward.setAwardName(activityLotteryAwardInDTO.getAwardName());
		activityLotteryAward.setAwardProb(activityLotteryAwardInDTO.getAwardProb());
		activityLotteryAward.setGmtCreate(new Date());
		activityLotteryAward.setLotteryId(activityLotteryAwardInDTO.getLotteryId());
		activityLotteryAward.setRemainCount(activityLotteryAwardInDTO.getRemainCount());
		activityLotteryAwardMapper.updateByPrimaryKeySelective(activityLotteryAward);
		baseOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
		return baseOutDTO;
	}


	@Override
	public ActivityLotteryListOutDTO activityLotteryRecordList(ActivityLotteryRecordInDTO activityLotteryRecordInDTO,
			PageDTO pageDTO) throws ApplicationException {
		ActivityLotteryListOutDTO activityLotteryListOutDTO=new ActivityLotteryListOutDTO();
		Page page = Tools.pageEmtity(pageDTO);
		activityLotteryRecordInDTO.setPage(page);
		
		ArrayList<ActivityLotteryRecord> activityLotteryRecordList=activityLotteryRecordMapper.selectActivityLotterylistPage(activityLotteryRecordInDTO);
		activityLotteryListOutDTO.setActivityLotteryRecordList(activityLotteryRecordList);
		activityLotteryListOutDTO.setPage(page);
		
		return activityLotteryListOutDTO;
	}


	@Override
	public ActivityLotteryAward activityLotteryAwardId(Long id) throws ApplicationException {
		ActivityLotteryAward activityLotteryAward=activityLotteryAwardMapper.selectByPrimaryKey(id);
		return activityLotteryAward;
	}


	@Override
	public BaseOutDTO updateVerifyAwards(Integer id) throws ApplicationException {
		BaseOutDTO baseOutDTO=new BaseOutDTO();
		ActivityLotteryRecord activityLotteryRecord=activityLotteryRecordMapper.selectByPrimaryKey(id);
		activityLotteryRecord.setIsGet(1);
		activityLotteryRecord.setGmtModified(new Date());
		activityLotteryRecordMapper.updateByPrimaryKeySelective(activityLotteryRecord);
		baseOutDTO.setErrorCode(Rescode.SUCCESS, "保存成功");
		return baseOutDTO;
	}
	
	
	
	
	
	
	

}
